package com.selftaught.service.impl;

import com.selftaught.domain.HospitalUser;
import com.selftaught.domain.hcpatientinfo.HcpatientinfoResponse;
import com.selftaught.domain.hcpatientinfo.XmlRequestBuilder;
import com.selftaught.domain.hcpatientinfo.XmlResponseParser;
import com.selftaught.service.IHospitalUserService;
import com.selftaught.utils.DateUtils;
import com.selftaught.utils.HttpXmlUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 医院患者 Service 业务层处理
 *
 * @author huya
 * @date 2025-10-23
 */
@Service
public class IHospitalUserServiceImpl implements IHospitalUserService {

    @Autowired
    private XmlRequestBuilder xmlRequestBuilder;

    @Autowired
    private XmlResponseParser xmlResponseParser;

    @Autowired
    private HttpXmlUtils httpXmlUtils;

    /**
     * 扫码新增医院患者
     *
     * @param keyword 关键字
     * @return 结果
     */
    @Override
    public HospitalUser insertHospitalUserByScanCode(String keyword) {
        HcpatientinfoResponse.PatientItem patientInfo = getPatientInfo(keyword);
        if (patientInfo != null) {
            HospitalUser hospitalUser = new HospitalUser();
            hospitalUser.setId(System.currentTimeMillis());
            hospitalUser.setPatMobile(patientInfo.getPatMobile());
            hospitalUser.setPatCardNo(patientInfo.getPatCardNo());
            hospitalUser.setPatientId(patientInfo.getPatientId());
            hospitalUser.setPatSex(patientInfo.getPatSex());
            hospitalUser.setPatidType(patientInfo.getPatIdType());
            hospitalUser.setPatName(patientInfo.getPatName());
            hospitalUser.setPatCardType(patientInfo.getPatCardType());
            hospitalUser.setPatAge(patientInfo.getPatAge());
            hospitalUser.setPatType(patientInfo.getPatType());
            hospitalUser.setPatAddress(patientInfo.getPatAddress());
            hospitalUser.setPatidNo(patientInfo.getPatIdNo());
            hospitalUser.setTotalAmt(patientInfo.getTotalAmt());
            hospitalUser.setPatBirth(DateUtils.parseDate(patientInfo.getPatBirth()));
            return hospitalUser;
        }
        return null;
    }

    /**
     * 调用远程接口获取患者信息
     *
     * @param itemNo 项目编号 (病历号)
     * @return 患者信息响应对象
     */
    public HcpatientinfoResponse.PatientItem getPatientInfo(String itemNo) {
        try {
            // 构建 XML 请求
            String xmlRequest = xmlRequestBuilder.buildRequestXml(itemNo);
            // 发送请求
            String xmlResponse = httpXmlUtils.postXml(HospitalUser.API_URL, xmlRequest);
            // 解析响应
            HcpatientinfoResponse hcpatientinfoResponse = xmlResponseParser.parseResponse(xmlResponse);
            if (hcpatientinfoResponse != null) {
                HcpatientinfoResponse.ResponseBody body = hcpatientinfoResponse.getBody();
                if (body != null) {
                    HcpatientinfoResponse.ResponseBody.Items items = body.getItems();
                    if (items != null) {
                        List<HcpatientinfoResponse.PatientItem> item = items.getItem();
                        if (!item.isEmpty()) {
                            HcpatientinfoResponse.PatientItem patientInfoItem = item.get(0);
                            if (patientInfoItem != null) {
                                return patientInfoItem;
                            }
                        }
                    }
                }
            }
            throw new RuntimeException("未获取到患者信息，请重新扫码");
        } catch (Exception e) {
            throw new RuntimeException("未获取到患者信息，请重新扫码");
        }
    }

}
